<?php

namespace Organize\Controllers\System;

use Illuminate\Http\Request;
use Platform\Controller;
use Platform\System\Tools\ResultTool;
use Platform\System\Tools\Upload\UploadsOrganize;

/**
 * @module 文件资源
 * Class UploadController
 * @package Admin\Controllers\System
 */
class UploadController extends Controller
{
    public $upManager;
    function __construct(UploadsOrganize $manager)
    {
        $this->upManager = $manager;
    }

    /**
     * @permission 上传图片
     * @param Request $request
     * @return mixed
     */
    public function uploadImg(Request $request){

        $data = $this->upManager->uploadImg($request);

        $result = myResult()->setData($data);

        if($data['count_success']) {

            if ($data['use_type'] == 'organize_editor'){
                /*
                 * fixme 如果是编辑器则返回另外的格式
                 */
                $url = [];
                foreach ($data['data'] as $val){
                    $url[] = $val['url'];
                }
                $data['errno'] = 0;
                $data['data'] = $url;
                return $data;
            }

            return $result->setCode(ResultTool::CODE_SUCCESS,'上传成功')->send();
        }
        return $result->setCode(ResultTool::CODE_TARGET_WRONG,'上传失败')->send();
    }


}
